package game.main;

import engine.input.Key;
import javafx.geometry.Bounds;
import javafx.scene.shape.Rectangle;

import static engine.app.View.keyH;
import static game.basic.GameBase.*;
import static game.main.GameApp.gameMusic;
import static game.main.GameApp.player;
import static game.main.GamePanel.*;

public class EventHandler {

    EventRect[][][] eventRect;
    int eventRectX, eventRectY, eventRectWidth, eventRectHeight;

    //设置为不可连续触发的属性
    int previousEventX, previousEventY;
    boolean canTouchEvent = true;

    Rectangle rectangle;

    public EventHandler() {
        eventRect = new EventRect[maxMap][maxWorldCol][maxWorldRow];

        int map = 0;
        int col = 0;
        int row = 0;

        while (map < maxMap && col < maxWorldCol && row < maxWorldRow) {
            eventRect[map][col][row] = new EventRect();
            eventRect[map][col][row].x = 16;
            eventRect[map][col][row].y = 16;

            col++;
            if (col == maxWorldCol) {
                col = 0;
                row++;
                if (row == maxWorldRow) {
                    row = 0;
                    map++;
                }
            }
        }
        eventRectX = 16;
        eventRectY = 16;
        eventRectWidth = 16;
        eventRectHeight = 16;
        rectangle=new Rectangle();
    }

    public void checkEvent() {
        int xDistance = Math.abs(player.worldX - previousEventX);
        int yDistance = Math.abs(player.worldY - previousEventY);
        int distance = Math.max(xDistance, yDistance);
        if (distance > tileSize) {
            canTouchEvent = true;
        }
        if (canTouchEvent) {
            if (hit(0, 26, 16, rightDir)) {
                damagePit(0, 26, 16, dialogueState);
            }
            if (hit(0, 27, 16, rightDir)) {
                damagePit(dialogueState, false);
            }
            if (hit(0, 23, 12, upDir)) {
                healingPoll(dialogueState);
            }
            if (hit(0, 19, 16, leftDir)) {
                teleport(dialogueState);
            }
        }
    }

    //陷阱事件
    public void damagePit(int map, int col, int row, int gameStateNow) {
        gameState = gameStateNow;
        gameMusic.playSE(6);
        ui.currentDialogue = "你踩到了陷阱";
        player.hp -= 1;
        eventRect[map][col][row].eventDone = true;//设置为true后，该方块不可再触发事件 只能触发单次
    }

    public void damagePit(int gameStateNow, boolean can) {
        gameState = gameStateNow;
        gameMusic.playSE(6);
        ui.currentDialogue = "你踩到了陷阱!!!!!";
        player.hp -= 1;
        canTouchEvent = can;//设置为多次不可连续触发 只有离开该方块时才可以再次触发
    }


    //治疗池事件
    public void healingPoll(int gameStateNow) {
        if (keyH.isPressed(Key.Z)||keyH.isPressed(Key.Enter)) {
            gameState = gameStateNow;
            gameMusic.playSE(2);
            ui.currentDialogue = "重生池\n生命值回复 法力回复\n怪物重生";
            player.hp = player.maxHp;
            aSetter.setMonster();
        }
    }

    //传送事件
    public void teleport(int gameStateNow) {
        gameState = gameStateNow;
        gameMusic.playSE(2);
        ui.currentDialogue = "传送";
        player.worldX = tileSize * 37;
        player.worldY = tileSize * 10;
    }


    //设置的格子的碰撞检测
    public boolean hit(int map, int col, int row, int reqDirection) {
        boolean hit = false;
        player.solidAreaBoxX = player.worldX + player.solidAreaX;
        player.solidAreaBoxY = player.worldY + player.solidAreaY;
        eventRectX = col * tileSize + eventRect[map][col][row].x;
        eventRectY = row * tileSize + eventRect[map][col][row].y;

        if (player.getRectangle().intersects(getRectangle()) && !eventRect[map][col][row].eventDone) {
            if (player.direction == reqDirection || reqDirection == -999) {//-999表示不要求方向
                hit = true;
                previousEventX = player.worldX;
                previousEventY = player.worldY;
            }
        }
        return hit;
    }


    public Bounds getRectangle() {//return一个轮廓出去 用于碰撞检测
        rectangle.setX(eventRectX);
        rectangle.setY(eventRectY);
        rectangle.setWidth(eventRectWidth);
        rectangle.setHeight(eventRectHeight);
        return rectangle.getBoundsInLocal();
    }


}
